package org.jeecg.modules.bobo.common.util;

import com.upyun.RestManager;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传工具类(又拍云)
 * @program: jeecg-boot-parent
 * @description:
 * @author: 三千霜
 * @create: 2020-11-06 19:42
 */
@Slf4j
@Component("boboUpyunUtil")
public class UpyunUtil {

    private static RestManager restManager;

    private static String bizPath;

    private static String upyunUrl;
    @Autowired
    public static void setRestManager(RestManager restManager) {
        UpyunUtil.restManager = restManager;
    }
    @Value(value = "${jeecg.bizPath}")
    public static void setBizPath(String bizPath) {
        UpyunUtil.bizPath = bizPath;
    }
    @Value(value = "${jeecg.upyun.upyun_url}")
    public static void setUpyunUrl(String upyunUrl) {
        UpyunUtil.upyunUrl = upyunUrl;
    }

    /**
     * 上传文件至阿里云 OSS
     * 文件上传成功,返回文件完整访问路径
     * 文件上传失败,返回 null
     *
     * @param file    待上传文件
     * @param fileDir 文件保存目录
     * @return oss 中的相对文件路径
     */
    public static String upload(MultipartFile file, String fileDir,String customBucket) {
        //生成文件名
        String FILE_URL = null;
        fileDir=UpyunUtil.bizPath;
        StringBuilder fileUrl = new StringBuilder();
        try {
            // 获取文件名
            String orgName = file.getOriginalFilename();
            if("" == orgName){
                orgName=file.getName();
            }
            orgName = CommonUtils.getFileName(orgName);

            //按时间生成文件夹及文件名
            String fileName = DateUtils.formatDate(DateUtil.getDate())+"/"+String.valueOf(DateUtils.getMillis()) + orgName.substring(orgName.indexOf("."));
            if (!fileDir.endsWith("/")) {
                fileDir = fileDir.concat("/");
            }
            fileUrl = fileUrl.append(fileDir + fileName);

            if (StringUtils.isNotEmpty(upyunUrl) && upyunUrl.toLowerCase().startsWith("http")) {
                FILE_URL = upyunUrl + fileUrl.toString();
            }

            Response response = restManager.writeFile(fileUrl.toString(),file.getInputStream(), null);
            if (!isSuccess(response)){
//                autoServiceLogService.setLog("Upyunerror",fileUrl.toString(),response.body().string());
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
//            return e.getMessage();
            return null;
        }
        return FILE_URL;
    }

    /**
     * 文件上传
     * @param file
     * @param fileDir
     * @return
     */
    public static String upload(MultipartFile file, String fileDir) {
        return upload(file, fileDir,null);
    }

    /**
     * 上传文件至阿里云 OSS
     * 文件上传成功,返回文件完整访问路径
     * 文件上传失败,返回 null
     *
     * @param file    待上传文件
     * @param fileDir 文件保存目录
     * @return oss 中的相对文件路径
     */
    public  static String upload(FileItemStream file, String fileDir) {

        //生成文件名
        String FILE_URL = null;
        fileDir=bizPath;
        StringBuilder fileUrl = new StringBuilder();
        try {
            // 获取文件名
            String orgName = "";
            if("" == orgName){
                orgName=file.getName();
            }
            orgName = CommonUtils.getFileName(orgName);

            //按时间生成文件夹及文件名
            String fileName = DateUtil.formatDate(DateUtil.getDate())+"/"+ String.valueOf(DateUtils.getMillis()) + orgName.substring(orgName.indexOf("."));
            if (!fileDir.endsWith("/")) {
                fileDir = fileDir.concat("/");
            }
            fileUrl = fileUrl.append(fileDir + fileName);

            if (StringUtils.isNotEmpty(upyunUrl) && upyunUrl.toLowerCase().startsWith("http")) {
                FILE_URL = upyunUrl + fileUrl.toString();
            }
            Response response = restManager.writeFile(fileUrl.toString(), file.openStream(), null);
            if (!isSuccess(response)){
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return FILE_URL;
    }

    private static Boolean isSuccess(Response response) {
        return response.isSuccessful() ? true: false;
    }

}
